/**
 * 
 */
package com.xbang.attendance.entity.system;

import java.io.Serializable;
import java.util.Iterator;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Transient;

/**
 * 仓库
 * 
 * @author dev.zhang
 * 
 */
@Entity
public class Warehouse implements Serializable {

	private static final long serialVersionUID = -1149851104001066038L;

	/** 仓库编码 */
	private String code;
	/** 仓库名称 */
	private String name;

	/** 负责人 */
	private String person;

	/** 联系电话 */
	private String telephone;

	/** 联系email */
	private String email;

	@OneToOne()
	@JoinColumn(name = "region", referencedColumnName = "id")
	private Region region;

	@OneToMany(fetch = FetchType.EAGER, mappedBy = "warehouse", cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH }, targetEntity = WarehouseProductAgreement.class)
	private Set<WarehouseProductAgreement> warehouseProductAgreements;

	/** 地址 */
	private String address;

	/** 描述 */
	private String description;

	private String fax;

	@Transient
	private boolean selectedByMaintenance;

	@Transient
	private MaintenanceStation tmpMaintenanceStation;

	@ManyToMany(mappedBy = "warehouse", targetEntity = MaintenanceStation.class, fetch = FetchType.EAGER)
	private Set<MaintenanceStation> maintenances;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Integer id;

	/*排序*/
	@Transient
	private Integer order;
	/**
	 * @return the id
	 */
	public Integer getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	/**
	 * @return the code
	 */
	public String getCode() {
		return code;
	}

	/**
	 * @param code
	 *            the code to set
	 */
	public void setCode(String code) {
		this.code = code;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the person
	 */
	public String getPerson() {
		return person;
	}

	/**
	 * @param person
	 *            the person to set
	 */
	public void setPerson(String person) {
		this.person = person;
	}

	/**
	 * @return the telephone
	 */
	public String getTelephone() {
		return telephone;
	}

	/**
	 * @param telephone
	 *            the telephone to set
	 */
	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}

	/**
	 * @return the address
	 */
	public String getAddress() {
		return address;
	}

	/**
	 * @param address
	 *            the address to set
	 */
	public void setAddress(String address) {
		this.address = address;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description
	 *            the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * The default constructor
	 */
	public Warehouse() {

	}

	/**
	 * @return the email
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * @param email
	 *            the email to set
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * @return the maintenances
	 */
	public Set<MaintenanceStation> getMaintenances() {
		return maintenances;
	}

	/**
	 * @param maintenances
	 *            the maintenances to set
	 */
	public void setMaintenances(Set<MaintenanceStation> maintenances) {
		this.maintenances = maintenances;
	}

	/**
	 * @return the fax
	 */
	public String getFax() {
		return fax;
	}

	/**
	 * @param fax
	 *            the fax to set
	 */
	public void setFax(String fax) {
		this.fax = fax;
	}

	/**
	 * @return the selectedByMaintenance
	 */
	public boolean isSelectedByMaintenance() {
		for (Iterator<MaintenanceStation> iterators = maintenances.iterator(); iterators.hasNext();) {
			MaintenanceStation maintenanceStation = iterators.next();
			if (this.tmpMaintenanceStation == null || this.tmpMaintenanceStation.getId() == null) {
				return false;
			}
			if (this.tmpMaintenanceStation.getId().equals(maintenanceStation.getId())) {
				return true;
			}
		}

		return false;
	}

	/**
	 * @param selectedByMaintenance
	 *            the selectedByMaintenance to set
	 */
	public void setSelectedByMaintenance(boolean selectedByMaintenance) {
		this.selectedByMaintenance = selectedByMaintenance;
	}

	/**
	 * @return the tmpMaintenanceStation
	 */
	public MaintenanceStation getTmpMaintenanceStation() {
		return tmpMaintenanceStation;
	}

	/**
	 * @param tmpMaintenanceStation
	 *            the tmpMaintenanceStation to set
	 */
	public void setTmpMaintenanceStation(MaintenanceStation tmpMaintenanceStation) {
		this.tmpMaintenanceStation = tmpMaintenanceStation;
	}

	/**
	 * @return the region
	 */
	public Region getRegion() {
		return region;
	}

	/**
	 * @param region
	 *            the region to set
	 */
	public void setRegion(Region region) {
		this.region = region;
	}

	/**
	 * @return the warehouseProductAgreements
	 */
	public Set<WarehouseProductAgreement> getWarehouseProductAgreements() {
		return warehouseProductAgreements;
	}

	/**
	 * @param warehouseProductAgreements
	 *            the warehouseProductAgreements to set
	 */
	public void setWarehouseProductAgreements(Set<WarehouseProductAgreement> warehouseProductAgreements) {
		this.warehouseProductAgreements = warehouseProductAgreements;
	}
	/***
	 * 重写hashcode使之在set中不重复
	 */
	@Override
	public int hashCode() {
		return this.getId().hashCode();
	}

	public Integer getOrder() {
		return order;
	}

	public void setOrder(Integer order) {
		this.order = order;
	}
	
}
